# -*- coding:utf-8 -*-
# created on 2017/4/30
#

from mathsolver.functions.base import *
from mathsolver.functions.base.base import new_latex
from mathsolver.functions.mathematica.mathematicaSolve import MathematicaSolve
from mathsolver.functions.process import ProcessExpression
from mathsolver.functions.xiangliang.Vector_SiBianXing.base import XLQuadraKnownUpdate, XLQuadraAxisConditionEqTransform
from mathsolver.functions.xiangliang.basic import xl_solutions_subs
from sympy import Abs, acos


class XLQuadraGetAngle001(BaseFunction):
    """
    在等腰梯形ABCD中,BC为底边,\\overrightarrow{AD}=t\\overrightarrow{BC},|\\overrightarrow{AB}|=2,|\\overrightarrow{AD}|=2,
    |\\overrightarrow{CD}|=2,|\\overrightarrow{BC}|=4,
    则向量\\frac{1}{2}\\overrightarrow{AD}与\\overrightarrow{BA}-\\overrightarrow{AD}的夹角"
    1.等腰三角形类
    2.vEqs非空
    3.坐标法
    """
    def solver(self, *args):
        p1, p2, p3, p4 = args[0].value
        name = p1 + p2 + p3 + p4
        assert name in self.known
        v_dytx = self.search(name)
        assert v_dytx.type == 'vIsOscelesTrapezoid'
        v_eqs = v_dytx.Eqs
        assert v_eqs
        vector_a = args[1].sympify()
        vector_b = args[2].sympify()
        cos_angle = vector_a * vector_b / (Abs(vector_a) * Abs(vector_b))
        u_eqs = [[cos_angle, 0]]
        u_eqs.extend(v_eqs)
        stepsolver = XLQuadraKnownUpdate(self.known).solver(u_eqs, v_dytx)
        self.steps += stepsolver.steps
        new_known = stepsolver.output[0]
        condition_veqs = []
        for eq in v_eqs:
            if len(eq) == 2:
                new_eqs = XLQuadraAxisConditionEqTransform(new_known).solver(BaseEq(eq)).output[0].sympify()
            else:
                new_eqs = XLQuadraAxisConditionEqTransform(new_known).solver(BaseIneq(eq)).output[0].sympify()
            condition_veqs.extend(new_eqs)
        self.steps.append(["", "依题意，得"])
        if len(condition_veqs) == 1:
            if len(condition_veqs[0]) == 2:
                self.steps.append(["", "%s" % BaseEq(condition_veqs[0]).printing()])
            else:
                self.steps.append(["", "%s" % BaseIneq(condition_veqs[0]).printing()])
        else:
            self.steps.append(["", "%s" % BaseIneqs(condition_veqs).printing()])
        solutions = MathematicaSolve().solver(BaseIneqs(condition_veqs)).output[0]
        self.steps.append(["", "解得: %s" % (solutions.printing())])
        new_cos_angle = ProcessExpression(new_known).solver(BaseValue(str(cos_angle))).output[0].sympify()
        new_cos_angle_values = xl_solutions_subs(solutions, new_cos_angle)
        angle_values = []
        for new_cos_Angle_value in new_cos_angle_values:
            angle_value = acos(new_cos_Angle_value)
            angle_values.append(angle_value)
        self.steps.append(["∴", " 或 ".join(["%s和%s的夹角为: %s" % (
            new_latex(vector_a), new_latex(vector_b), new_latex(v)) for v in angle_values])])
        self.output.append(BaseNumbers(angle_values))
        self.label.add("向量夹角-坐标法")
        return self


class XLQuadraGetAngle(BaseFunction):
    CLS = [XLQuadraGetAngle001]
    
    def solver(self, *args):
        known = self.known
        r = None
        for cl in XLQuadraGetAngle.CLS:
            try:
                r = cl(known, verbose=True).solver(*args)
                break
            except Exception:
                pass
        if not r:
            raise 'try fail'
        return r

